//
//  GPSTrainingInfoItemModel.swift
//  DaRings
//
//  Created by HFY on 2022/12/9.
//

import UIKit

open class GPSTrainingInfoItemModel: TrainingItemModel {
    
    public var imageName: String?
    
    public var speed: Double?
    
    
    public override var progress: Double? {
        guard let targetData = targetData else { return nil }
        if currentData == 0 || targetData == 0 {
            return nil
        }
        
        switch contentType {
            
        case .pace:
            // 目标配速是进度条的一半，配速为0时，进度条在最左侧，配速在0-目标配速时，在中心点右侧；配速在目标配速-目标配速*2时，在中心点左侧
            if currentData <= 0 {
                return 0.0
            }
            if currentData >= targetData * 2 {
                return 0.0
            }
            
            if currentData >= targetData {
                return 0.5 - (currentData - targetData) / (targetData * 2)
            }
            if currentData < targetData {
                return 0.5 + (targetData - currentData ) / (targetData * 2)
            }
            
            return 0.0
            
        default:
            return currentData/targetData
        }
        
    }
    
    public override var highProgress: Double? {
        
        switch contentType {
            
        case .pace:
            
            return 0.5
             
        default:
            return nil
        }
    }
    
    open func progress(of speed: Double?, data: Double) ->Double {
        
        if let speed = speed, speed > 0 {
            var progress: Double = 0
            progress = abs(data - speed) / (data * 2 / 50)
            if speed < data {
                progress += 50
            } else {
                progress = 50 - progress
                if progress < 0 {
                    progress = 0
                }
            }
            return progress
        }
        return 0
    }
    
      
}


extension GPSTrainingInfoItemModel {
    
    public static var distance: GPSTrainingInfoItemModel {
        
        return item(of: .distance)
    }
    
    public static var time: GPSTrainingInfoItemModel {
        
        return item(of: .time)
    }
    
    public static var pace: GPSTrainingInfoItemModel {
        
        return item(of: .pace)
    }
    
    public static var calorie: GPSTrainingInfoItemModel {
        
        return item(of: .calorie)
    }
    
    public static var heartRate: GPSTrainingInfoItemModel {
        
        let item = item(of: .heartRate)
        item.imageName = "ic_workout_popular_hr"
        return item
    }
    
    public static func item(of type: TrainingTypeContent) ->GPSTrainingInfoItemModel {
        
        let model = GPSTrainingInfoItemModel()
        model.contentType = type
        model.title = type.title
        if type == .calorie || type == .heartRate || type == .distance {
            model.title = type.targetUnit
        }
        if type != .time && type != .pace {
            model.unit = type.targetUnit
        }
        if type == .heartRate {
            model.imageName = "ic_workout_popular_hr"
        }
        
        
        return model
        
    }
    

    
}
